package com.soul.doc.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soul.common.bean.vo.BaseDataVo;
import com.soul.common.exception.BaseException;
import com.soul.common.service.BaseDataService;
import com.soul.common.utils.ListUtil;
import com.soul.doc.bean.dto.CommentDto;
import com.soul.doc.bean.dto.LikeCommentDto;
import com.soul.doc.bean.po.CommentPo;
import com.soul.doc.bean.vo.CommentVo;
import com.soul.doc.bean.vo.MusicVo;
import com.soul.doc.mapper.CommentMapper;
import com.soul.doc.service.CommentService;
import com.soul.common.service.BaseService;
import com.soul.doc.service.LikeCommentService;
import com.soul.doc.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sws
 * @since 2023-12-16 19:27:59
 */
@Service
public class CommentServiceImpl extends BaseService<CommentMapper, CommentPo> implements CommentService {


    @Autowired
    private BaseDataService baseDataService;
    @Autowired
    private LikeCommentService likeCommentService;
    @Autowired
    private MusicService musicService;
    /**
     * 根据歌曲Id,查询评论
     */
    @Override
    public IPage<CommentVo> queryComment(Integer page, Integer size, CommentDto commentDto){

        Page<CommentPo> objectPage = new Page<>(page, size);
        Page<CommentPo> poPage = this.getLambadaQueryWrapper()
                .eq(ObjectUtil.isNotEmpty(commentDto.getMusicId()), CommentPo::getMusicId, commentDto.getMusicId())
                .orderByDesc(CommentPo::getCreateTime)
                .page(objectPage);
        BaseDataVo baseDataVo = baseDataService.loadBaseData(poPage.getRecords(), (t, b) -> {
            b.addUserId(t.getCreateUser());
        });
        List<Long> ids = ListUtil.toValueList(poPage.getRecords(), t -> t.getId());
        Map<Long, List<Long>> likeMap = this.likeCommentService.queryUserMap(ids);
        List<Long> musicIds = ListUtil.toValueList(poPage.getRecords(), t -> t.getMusicId());
        List<MusicVo> musicVos = musicService.queryMusicByIds(musicIds);
        Map<Long, MusicVo> toMap = ListUtil.listToMap(musicVos, t -> t.getId());
        return poPage.convert(t-> {
            CommentVo data = this.toData(t, baseDataVo, likeMap);
            if (ObjectUtil.isNotEmpty(toMap)){
                data.setMusicVo(toMap.get(t.getMusicId()));
            }
            return data;
        });
    }

    public CommentVo toData(CommentPo po, BaseDataVo baseDataVo,Map<Long, List<Long>> likeMap){
        CommentVo commentVo = BeanUtil.copyProperties(po, CommentVo.class);
        commentVo.setUserVo(baseDataVo.getUserVo(po.getCreateUser()));
        List<Long> longs = likeMap.get(po.getId());
        commentVo.setLikeUserIds(longs);
        return commentVo;
    }
    public CommentVo toData(CommentPo po){
        BaseDataVo baseDataVo = new BaseDataVo();
        baseDataVo.addUserId(po.getCreateUser());
        baseDataService.loadBaseData(baseDataVo);
        Map<Long, List<Long>> likeMap = this.likeCommentService.queryUserMap(Collections.singletonList(po.getId()));
        return toData(po,baseDataVo,likeMap);
    }

    @Override
    public CommentVo addComment(CommentDto commentDto){
        if (ObjectUtil.isEmpty(commentDto.getContent())){
            throw new BaseException("请输入点内容！");
        }
        CommentPo commentPo = BeanUtil.copyProperties(commentDto, CommentPo.class);
        commentPo.setId(null);
        this.save(commentPo);
        return toData(commentPo);
    }

    @Override
    public void deleteComment(Long id){
        CommentPo byId = this.getById(id);
        if (ObjectUtil.isNotEmpty(byId)){
            this.removeById(id);
        }
    }

    @Override
    public CommentVo addLike(LikeCommentDto commentDto){
        CommentPo byId = this.getById(commentDto.getCommentId());
        long userId = StpUtil.getLoginIdAsLong();
        commentDto.setUserId(userId);
        boolean b = this.likeCommentService.addLikeComment(commentDto);
        if (b){
            // 是否增加或减少
            Integer likeNum = Optional.of(byId.getLikeNum()).orElse(0);
            if ("remove".equals(commentDto.getType())){
                likeNum = likeNum > 0? likeNum-1: 0;
            }else {
                likeNum = likeNum + 1;
            }
            byId.setLikeNum(likeNum);
            this.updateById(byId);
        }
        return this.toData(byId);
    }
}
